/**
 *
 */
package com.newer.controller.sys.menu;

import com.newer.biz.sys.auth.ShiroBiz;
import com.newer.biz.sys.menu.MenuInfoBiz;
import com.newer.commons.result.BizResult;
import com.newer.dao.bean.sys.Menu;
import com.newer.utils.ShiroAuthUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

/**
 * 菜单信息相关Controller
 *
 * @author xuntj
 * @create 2018-03-13 20:35
 **/
@Controller
@RequestMapping("sys/menu")
public class MenuController {

    /**
     * 注入菜单业务
     */
    @Autowired
    MenuInfoBiz menuInfoBiz;

    /**
     * 权限业务
     */
    @Autowired
    ShiroBiz shiroBiz;

    /**
     * 清除缓存
     */
    @Autowired
    ShiroFilterFactoryBean shiroFilterFactoryBean;

    /**
     * 菜单列表默认展示
     *
     * @return
     */
    @RequestMapping("/list.htm")
    public String menu() {
        return "sys/menu/menu_list";
    }

    /**
     * 菜单列表默认展示
     *
     * @return
     */
    @RequestMapping("/doList.do")
    @ResponseBody
    public String menuList(@RequestParam(defaultValue = "") String isMenu) {
        // 查询所有的菜单信息
        BizResult<Menu> bizResult = menuInfoBiz.queryAllMenus(isMenu);
        // 返回结果
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 菜单列表默认展示
     *
     * @return
     */
    @RequestMapping("/doDetail.do")
    @ResponseBody
    public String menuDetail(@RequestParam(defaultValue = "") String menuId) {
        // 查询所有的菜单信息
        BizResult<Menu> bizResult = menuInfoBiz.queryMenuInfoById(menuId);
        // 返回结果
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 菜单信息操作
     *
     * @return
     */
    @RequestMapping("/doOperate.do")
    @ResponseBody
    public String menuEdit(@RequestParam(value = "permissionIds[]", required = false) String[] permissionIds,
                           @RequestParam(value = "permissionIds", required = false) String permissionId,
                           Menu menu) {
        // 查询所有的菜单信息
        BizResult<?> bizResult = menuInfoBiz.operateMenuInfo(menu, permissionIds != null ? Arrays.asList(permissionIds) : (StringUtils.isNotBlank(permissionId) ? Arrays.asList(permissionId) : null));

        if (bizResult.isSuccess()) {
            ShiroAuthUtils.updatePermission(shiroFilterFactoryBean, menuInfoBiz, shiroBiz);
        }
        // 返回结果
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 菜单信息默认展示
     *
     * @return
     */
    @RequestMapping("/menuOperate.htm")
    public String menuOperate(String menuId, ModelMap map, String pid) {
        // 查询所有的菜单信息
        if (StringUtils.isNotBlank(menuId)) {
            BizResult<?> bizResult = menuInfoBiz.queryMenuInfoById(menuId);
            if (bizResult.isSuccess()) {
                map.put("menu", bizResult.getResult());
            }
        }
        map.put("pid", pid);
        return "sys/menu/menu_operate";
    }

    /**
     * 菜单列表默认展示
     *
     * @return
     */
    @RequestMapping("/doDel.do")
    @ResponseBody
    public String menuDel(String menuId) {
        // 查询所有的菜单信息
        BizResult<?> bizResult = menuInfoBiz.delMenuInfoById(menuId);
        // 返回结果
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 删除角色-菜单信息
     *
     * @return
     */
    @RequestMapping("/doDelRoleMenu.do")
    @ResponseBody
    public String roleMenuDel(String roleMenuId) {
        // 查询所有的菜单信息
        BizResult<?> bizResult = menuInfoBiz.delRoleMenuInfoById(roleMenuId);
        // 返回结果
        return JSONObject.fromObject(bizResult).toString();
    }
}
